//package com.fy.market.strategy.service.rule.filter.impl;
//
//import com.fy.market.strategy.model.entity.RuleActionEntity;
//import com.fy.market.strategy.model.entity.RuleMatterEntity;
//import com.fy.market.strategy.model.entity.StrategyAwardRuleEntity;
//import com.fy.market.strategy.model.enums.RuleLogicCheckTypeEnum;
//import com.fy.market.strategy.model.enums.RuleModelEnum;
//import com.fy.market.strategy.model.enums.RuleModelTypeEnum;
//import com.fy.market.strategy.repository.IStrategyAwardRepository;
//import com.fy.market.strategy.service.annotation.LogicStrategy;
//import com.fy.market.strategy.service.rule.filter.ILogicFilter;
//import com.fy.market.types.common.utils.FyObjUtil;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.util.List;
//
///**
// * 中置锁规则过滤
// *
// * @author fwq
// * @description 锁规则过滤
// * @date 2025-09-05
// * @Target(TYPE)
// * @Retention(RUNTIME)
// */
//@Service
//@LogicStrategy(ruleModel = RuleModelEnum.RULE_LOCK)
//public class RuleLockLogicFilter implements ILogicFilter {
//
//    @Resource
//    private IStrategyAwardRepository iStrategyAwardRepository;
//
//    @Override
//    public RuleActionEntity<RuleActionEntity.RaffleCenterEntity> ruleFilter(RuleMatterEntity ruleMatterEntity) {
//
//        // 构建响应
//        RuleActionEntity<RuleActionEntity.RaffleCenterEntity> ruleActionEntity = RuleActionEntity.<RuleActionEntity.RaffleCenterEntity>builder()
//                .ruleModelEnum(RuleModelEnum.RULE_LOCK)
//                .ruleLogicCheckTypeEnum(RuleLogicCheckTypeEnum.ALLOW)
//                .data(new RuleActionEntity.RaffleCenterEntity(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getAwardId()))
//                .build();
//
//        // 锁规则为空，直接放行
//        if (ruleMatterEntity.getRuleModel() == null || !RuleModelEnum.RULE_LOCK.equals(ruleMatterEntity.getRuleModel()))
//            return ruleActionEntity;
//
//        List<StrategyAwardRuleEntity> strategyAwardRuleEntities = iStrategyAwardRepository.queryStrategyAwardRuleEntities(ruleMatterEntity.getStrategyId());
//
//        if (FyObjUtil.isNullObj(strategyAwardRuleEntities)) return ruleActionEntity;
//
//        Integer awardUnlockCount = strategyAwardRuleEntities.stream()
//                .filter(strategyAwardRuleEntity -> ruleMatterEntity.getAwardId().equals(strategyAwardRuleEntity.getAwardId()))
//                .findFirst()
//                .map(strategyAwardRuleEntity -> {
//                    // 检查是否有抽奖中规则且为奖品锁规则
//                    boolean hasLockRule = strategyAwardRuleEntity.getRuleModels().stream()
//                            .anyMatch(ruleModelEntity ->
//                                    RuleModelTypeEnum.DURING_LOTTERY.getCode().equals(ruleModelEntity.getRuleModelType())
//                                            && RuleModelEnum.RULE_LOCK.getName().equals(ruleModelEntity.getRuleModel())
//                            );
//
//                    // 如果有锁规则，返回对应的awardSubtitle
//                    if (hasLockRule)
//                        return strategyAwardRuleEntity.getAwardUnlockCount();
//                    // 如果没有锁规则，直接放行（返回null或空字符串）
//                    return null;
//                })
//                .orElse(null);
//
//        Integer userUnlockCount = 2;  //模拟用户抽奖次数统计，后续是从缓存获取数据库获取，目前先定死方便开发
//
//        // 如果awardUnlockCount不为空，说明有锁规则，看看用户抽奖次数是否已达到，达到的话放行，不达到的话拦截
//        if (awardUnlockCount == null || userUnlockCount >= awardUnlockCount)
//            // 解锁成功，返回允许通过
//            return ruleActionEntity;
//
//        // 解锁失败，返回拒绝通过
//        ruleActionEntity.setRuleLogicCheckTypeEnum(RuleLogicCheckTypeEnum.TAKE_OVER);
//
//        return ruleActionEntity;
//
//    }
//}
